package com.feicheng.iou.controller;


import com.feicheng.iou.bean.InformationBean;
import com.feicheng.iou.commom.ReturnCode;
import com.feicheng.iou.constant.StatusCodeConstant;
import com.feicheng.iou.exception.SystemException;
import com.feicheng.iou.exception.SystemExceptionV110;
import com.feicheng.iou.pojo.InterviewPOJO;
import com.feicheng.iou.pojo.ResourcePOJO;
import com.feicheng.iou.result.ResultInfo;
import com.feicheng.iou.service.InterviewService;
import com.feicheng.iou.util.IdempotentUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author tianchunlei
 * @since 2021-06-28
 */
@RestController
@RequestMapping("/iou/interview")
@Slf4j
@CrossOrigin
public class InterviewController {

    @Autowired
    private InterviewService interviewService;

    /*
     * @Author luoxin
     * @Description //添加到访线索的接口
     * @Date 下午5:32 2021/7/13
     * @Param ResourcePOJO
     * @return ResultInfo
     */
    @PostMapping
    public ResultInfo addInterview(InterviewPOJO interviewPOJO) {   //转到访
        log.info("addInterview方法进入，参数为: " + interviewPOJO);

        //判断请求是否重复
        if (!IdempotentUtils.judge(interviewPOJO, this.getClass())){
            throw  new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F004,"线索添加"));
        }
        //参数校验是否为空
        if (interviewPOJO == null) {
            throw new SystemException(ReturnCode.PARAM_NOT_NULL);
        }

        //调用interviewService实现线索添加业务逻辑
        return interviewService.insert(interviewPOJO);
    }

    /*
     * @Author luoxin
     * @Description //修改到访线索的接口
     * @Date 下午5:32 2021/7/13
     * @Param ResourcePOJO
     * @return ResultInfo
     */
    @PutMapping
    public ResultInfo updateInterview(InterviewPOJO interviewPOJO) {  //修改到访线索
        log.info("updateInterview方法进入，参数为: " + interviewPOJO);

        // 参数校验是否为空
        if (interviewPOJO == null) {
            throw new SystemException(ReturnCode.PARAM_NOT_NULL);
        }

        //调用interviewService实现线索修改业务逻辑
        return interviewService.update(interviewPOJO);
    }


    /*
     * @Author luoxin
     * @Description //删除到访线索的接口
     * @Date 下午5:32 2021/7/13
     * @Param ResourcePOJO
     * @return ResultInfo
     */
    @DeleteMapping
    public ResultInfo deleteResource(@RequestBody InterviewPOJO interviewPOJO) {  //删除到访线索
        log.info("deleteResource方法进入，参数为: " + interviewPOJO);

        // 参数校验是否为空
        if (interviewPOJO == null) {
            throw new SystemException(ReturnCode.PARAM_NOT_NULL);
        }

        //调用interviewService实现线索删除业务逻辑
        return interviewService.delete(interviewPOJO);

    }


    /*
     * @Author luoxin
     * @Description //查询到访线索列表的接口
     * @Date 下午5:32 2021/7/13
     * @Param ResourcePOJO
     * @return ResultInfo
     */
    @GetMapping("/list")
    public ResultInfo selectInterviewList(InterviewPOJO interviewPOJO, HttpServletRequest request){  //查询到访线索列表
        log.info("selectInterviews方法进入，参数为: " + interviewPOJO);

        // 参数校验是否为空
        if (interviewPOJO == null) {
            throw new SystemException(ReturnCode.PARAM_NOT_NULL);
        }

        //调用interviewService实现查询列表业务
        return interviewService.listAll(interviewPOJO,request);
    }

    /*
     * @Author luoxin
     * @Description //查询单个到访线索的接口
     * @Date 下午5:32 2021/7/13
     * @Param id
     * @return ResultInfo
     */
    @GetMapping("/info/{id}")
    public ResultInfo selectinfo(@PathVariable("id") Integer id) {  //查询单个到访线索
        log.info("selectinfo方法进入，参数为: " + id);

        // 参数校验是否为空
        if (id == null) {
            throw new SystemException(ReturnCode.PARAM_NOT_NULL);
        }

        //调用interviewService实现线索单个查询业务逻辑
        return interviewService.selectinfo(id);
    }

    /*
     * @Author luoxin
     * @Description //分配到访线索的接口
     * @Date 下午5:32 2021/7/13
     * @Param ResourcePOJO
     * @return ResultInfo
     */
    @PutMapping("/autor")
    public ResultInfo updateAutor(InterviewPOJO interviewPOJO) {  //分配到访线索
        log.info("updateAutor方法进入，参数为: " + interviewPOJO);

        // 参数校验是否为空
        if (interviewPOJO == null) {
            throw new SystemException(ReturnCode.PARAM_NOT_NULL);
        }

        //调用interviewService实现线索分配业务逻辑
        return interviewService.updateAutor(interviewPOJO);
    }
    /**
     * @Author luoxin
     * @Description 放弃线索
     * @Date 5:03 下午 2021/7/27
     * @param interviewPOJO
     * @return ResultInfo
     */
    @PutMapping("/abandon")
    public ResultInfo abandonInterview(InterviewPOJO interviewPOJO) {  //放弃线索
        log.info("updateAutor方法进入，参数为: " + interviewPOJO);

        // 参数校验是否为空
        if (interviewPOJO == null) {
            throw new SystemException(ReturnCode.PARAM_NOT_NULL);
        }

        //调用interviewService实现线索分配业务逻辑
        return interviewService.abandonInterview(interviewPOJO);
    }

    /**
     * @Author luoxin
     * @Description 修到访线索Flg
     * @Date 4:59 下午 2021/7/27
     * @param interviewPOJO
     * @return
     */
    @PutMapping("/flag")
    public ResultInfo updateFlg(InterviewPOJO interviewPOJO) {  //到访有效开关
        log.info("InterviewController的updateFlg，参数interviewPOJO: " + interviewPOJO);

        //参数校验是否为空
        if (interviewPOJO == null) {
            throw new SystemException(ReturnCode.PARAM_NOT_NULL);
        }
        //调用ResourceService实现线索分配业务逻辑
        return interviewService.updateFlg(interviewPOJO);
    }
}

